//
//  Bx_dznEmptyDelegate.m
//  BiXuan
//
//  Created by ayi on 2018/7/27.
//  Copyright © 2018年 BSNL. All rights reserved.
//

#import "Custom_dznEmptyDelegate.h"

#import "ZY_AfnNetManager.h"
@interface Custom_dznEmptyDelegate()

@property (nonatomic, weak) UIScrollView *scroll;

@property (nonatomic, copy) dznEmptyDelegateBtnCallBack btncallback;
@property (nonatomic, copy) dznEmptyDelegateFreshCallBack freshcallback;
@property (nonatomic, copy) dznEmptyDelegateOtherCallBack othercallback;

@property (nonatomic, assign) Custom_dznEmptyCallbackType callbackType;
@property (nonatomic, assign) Custom_dznEmptyAdminState emptyAdminState;//默认结束状态

@end

@implementation Custom_dznEmptyDelegate

- (void)dealloc {
    NSLog(@"=========%@", NSStringFromClass(self.class));
}

- (instancetype)initWithEmptyTitle:(NSString *)emptyStr
                        andImgName:(NSString *)emptyImg
                        andBtnName:(NSString *)emptyBtnTitle
                 andverticalOffset:(CGFloat )offset
                         andScroll:(UIScrollView *)scroll
                       andCallBack:(dznEmptyDelegateBtnCallBack)callback
                  andFreshCallBack:(dznEmptyDelegateFreshCallBack)freshcallback
                  andOtherCallBack:(dznEmptyDelegateOtherCallBack)othercallback
{
    self = [super init];
    if (self) {
        _emptyStr = emptyStr;
        _emptyImg = emptyImg;
        _offset = offset;
        _emptyBtnTitle = emptyBtnTitle;
        _emptyVisable = YES;
        
        scroll.emptyDataSetDelegate = self;
        scroll.emptyDataSetSource = self;
        self.shouldScrollview = NO;
        self.callbackType = Custom_dznEmptyCallbackType_Normal;
        self.emptyAdminState = Custom_dznEmptyAdminState_EndFresh;
        self.shouldReloadAuto = YES;
        self.scroll = scroll;
        self.btncallback = callback;
        self.freshcallback = freshcallback;
        self.othercallback = othercallback;
        @weakify(self);
        [[[RACObserve([ZY_AfnNetManager shareInstance], networkStats)takeUntil:self.rac_willDeallocSignal]skip:1] subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            [self defaultDataRoload:[x integerValue]];
        }];
        [ZY_AfnNetManager startMonitoring];
    }
    return self;
}

-(void)reloadDznEmpty{
    [self.scroll reloadEmptyDataSet];
}

- (void)willbeginStartAdmination{
    self.emptyAdminState = Custom_dznEmptyAdminState_BeginFresh;
    [self reloadDznEmpty];
}

- (void)stopbeginStartAdmination{
    self.emptyAdminState = Custom_dznEmptyAdminState_EndFresh;
    [self reloadDznEmpty];
}

- (void)defaultDataRoload:(NSInteger )x{
    self.emptyAdminState = Custom_dznEmptyAdminState_EndFresh;
    
    if (x != StatusReachableViaWAN &&
        x != StatusReachableViaWiFi ) {
        self.callbackType = Custom_dznEmptyCallbackType_NoNetwork;
        [self reloadDznEmpty];
    }else{
        if (self.callbackType == Custom_dznEmptyCallbackType_NoNetwork) {
            self.callbackType = Custom_dznEmptyCallbackType_Normal;
            [self reloadDznEmpty];
        }
    }
}




#pragma mark ----------------------------------------------------------------------------------------------------------------
- (void)setAdminationStr:(NSString *)adminationStr{
    _adminationStr = adminationStr;
    self.emptyAdminState = Custom_dznEmptyAdminState_BeginFresh;
}

- (void)setResponseModel:(BaseNetResponseModel *)responseModel{
    _responseModel = responseModel;
    self.emptyAdminState = Custom_dznEmptyAdminState_EndFresh;
    
    if (_responseModel.code == 0 || _responseModel.code == 200) {
        _callbackType = Custom_dznEmptyCallbackType_Normal;
    }else if (_responseModel.code == -1009) {
        _callbackType = Custom_dznEmptyCallbackType_Refresh;
    }else{
        _callbackType = Custom_dznEmptyCallbackType_ResponseError;
    }
    [self.scroll reloadEmptyDataSet];
}

- (void)setEmptyVisable:(BOOL)emptyVisable{
    _emptyVisable = emptyVisable;
    [self.scroll reloadEmptyDataSet];
}


#pragma mark ----------------------------------------------------------------------------------------------------------------DZNEmptyDataSetSource
//提示语
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    if (self.emptyAdminState == Custom_dznEmptyAdminState_BeginFresh) {
        return nil;
    }
    NSString *titleStr;
    switch (self.callbackType) {
        case Custom_dznEmptyCallbackType_NoNetwork:
            titleStr = @"找不到网络啦";
            break;
        case Custom_dznEmptyCallbackType_Refresh:
            titleStr = @"暂无数据";
            break;
        case Custom_dznEmptyCallbackType_Normal:
            titleStr = _emptyStr;
            break;
        case Custom_dznEmptyCallbackType_ResponseError:
            titleStr = [NSString stringWithFormat:@"错误码：%ld \n信息：%@",(long)self.responseModel.code,self.responseModel.msg];
            break;
        default:
            break;
    }
    if (titleStr == nil) return nil;
    return [[NSAttributedString alloc]initWithString:titleStr attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:Common_TextLowColor}];
}

//图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    if (self.emptyAdminState == Custom_dznEmptyAdminState_BeginFresh) {
        return nil;
    }
    NSString *imgStr;
    switch (self.callbackType) {
        case Custom_dznEmptyCallbackType_NoNetwork:
            imgStr = @"img_network";
            break;
        case Custom_dznEmptyCallbackType_Refresh:
            imgStr = @"img_Empty-page";
            break;
        case Custom_dznEmptyCallbackType_Normal:
            imgStr = _emptyImg;
            break;
        case Custom_dznEmptyCallbackType_ResponseError:
            imgStr = @"img-error";
            break;
        default:
            break;
    }
    return imgStr?IMG(imgStr):nil;
}

//偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return _offset;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    if (self.emptyAdminState == Custom_dznEmptyAdminState_BeginFresh) {
        return nil;
    }
    NSString *btntitleStr;
    switch (self.callbackType) {
        case Custom_dznEmptyCallbackType_NoNetwork:
            btntitleStr = self.freshcallback == nil ? nil : @"刷新";
            break;
        case Custom_dznEmptyCallbackType_Refresh:
            btntitleStr = self.freshcallback == nil ? nil : @"刷新";
            break;
        case Custom_dznEmptyCallbackType_Normal:
            btntitleStr = self.btncallback == nil ? nil: _emptyBtnTitle;
            break;
        case Custom_dznEmptyCallbackType_ResponseError:
            btntitleStr = self.freshcallback == nil ? nil : @"刷新";
            break;
        default:
            break;
    }
    if (btntitleStr == nil) return nil;
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    [attributes setObject:Common_TextColor forKey:NSForegroundColorAttributeName];
    return [[NSAttributedString alloc] initWithString:btntitleStr attributes:attributes];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return self.shouldScrollview;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView{
    return self.emptyVisable;
}


//- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView{
//    return NO;
//}
//
//- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
//{
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
//    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
//    animation.duration = 0.35;
//    animation.cumulative = YES;
//    animation.repeatCount = MAXFLOAT;
//    return animation;
//}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    //加入你自定义的view
    if (self.emptyAdminState != Custom_dznEmptyAdminState_BeginFresh) {
        return nil;
    }
    if (self.animdelegate && [self.animdelegate respondsToSelector:@selector(customViewForEmptyAnimation)]) {
        return [self.animdelegate customViewForEmptyAnimation];
    }
    UIView *emptyView = [[UIView alloc]init];
    UIImage *img1 = IMG(@"ico_refresh_circle");
    UIImage *img2 = IMG(@"ico_refresh_logo");
    UIImageView *animationImgv = [[UIImageView alloc] initWithImage:img1];
    UIImageView *logoImgv = [[UIImageView alloc] initWithImage:img2];
    [emptyView addSubview:animationImgv];
    [emptyView addSubview:logoImgv];
    if (_adminationStr != nil) {
        UILabel *titleLab = [UILabel customInitLabWithFont:13 andTextColor:Common_TextLowColor andAlign:NSTextAlignmentCenter];
        [emptyView addSubview:titleLab];
        
        titleLab.frame = CGRectMake(0, CGRectGetMaxY(animationImgv.frame) + 20, [UIScreen mainScreen].bounds.size.width, 20);
        titleLab.text = _adminationStr;
    }
    animationImgv.frame = CGRectMake(0, 0, img1.size.width * 0.5, img1.size.height * 0.5);
    animationImgv.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, -40);
    logoImgv.frame = CGRectMake(0, 0, img2.size.width * 0.5, img2.size.height * 0.5);
    logoImgv.center = animationImgv.center;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
    animation.duration = 0.35;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    [animationImgv.layer addAnimation:animation forKey:@"animationimgv"];
    
    return emptyView;
}


- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
    UIImage *img = IMG(@"btn_blcak");
    return [[img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10.0, 0, 10.0) resizingMode:UIImageResizingModeStretch] imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, -img.size.width/1.5, 0, -img.size.width/1.5)];
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.backgroundColor != nil) {
        return self.backgroundColor;
    }
    return UIColor.whiteColor;
}
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
    return 20;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button{
    switch (self.callbackType) {
        case Custom_dznEmptyCallbackType_NoNetwork:
        case Custom_dznEmptyCallbackType_ResponseError:
        case Custom_dznEmptyCallbackType_Refresh:
            if (self.freshcallback) self.freshcallback();
            break;
        case Custom_dznEmptyCallbackType_Normal:
            if (self.btncallback) self.btncallback();
            break;
        case Custom_dznEmptyCallbackType_AppError:
        case Custom_dznEmptyCallbackType_Other:
            if (self.othercallback) self.othercallback(self.callbackType);
            break;
        default:
            break;
    }
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    if (self.shouldTapView) {
        if (self.othercallback) self.othercallback(self.callbackType);
    }
}



@end
